﻿<%@ WebHandler Language="C#" Class="ValidateCode" %>

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
using System.Collections;
public class ValidateCode : IHttpHandler , IRequiresSessionState{

    private static HttpContext context;

    public void ProcessRequest(HttpContext c)
    {
        context = c;
        string realIP = Web.Utils.GetIP();
        if (Security.Instance._ForbidIP.Contains(realIP))
        {
            context.Response.Write("-禁止访问-");
            context.Response.End();
        }
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        string str2 = ClsValidateCode.Generate();
        Web.Utils.SetSession("AdminCheckCode", str2);
      
    
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    internal class ClsValidateCode
    {
        private static Color BackgroundColor = Color.White;
        private static bool Chaos = true;
        private static Color ChaosColor = Color.LightGray;
        private static Color[] Colors = new Color[] { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
        private static string[] Fonts = new string[] { "Arial", "Georgia", "Catull Bold", "微软雅黑", "黑体", "隶书" };
        private static int FontSize = 14;
        private static int Padding = 2;
        private const double PI = 3.1415926535897931;
        private const double PI2 = 6.2831853071795862;

        public static Bitmap CreateImageCode(string code)
        {
            int fontSize = FontSize;
            int num2 = fontSize + Padding;
            int width = ((code.Length * num2) + 10) + 4;
            int height = (fontSize * 2) + Padding;
            Bitmap image = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(image);
            graphics.Clear(BackgroundColor);
            Random random = new Random();
            if (Chaos)
            {
                Pen pen = new Pen(ChaosColor, 0f);
                int num5 = Security.Instance.VCodeLength * 10;
                for (int j = 0; j < num5; j++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    graphics.DrawRectangle(pen, x, y, 1, 1);
                }
            }
            int num9 = 0;
            int num10 = 0;
            int num11 = 1;
            int num12 = 1;
            int num13 = (height - FontSize) - 4;
            int num14 = num13 / 4;
            num11 = num14;
            num12 = num14 * 2;
            for (int i = 0; i < code.Length; i++)
            {
                int index = random.Next(Colors.Length - 1);
                int num16 = random.Next(Fonts.Length - 1);
                Font font = new Font(Fonts[num16], (float)fontSize, FontStyle.Regular);
                Brush brush = new SolidBrush(Colors[index]);
                if ((i % 2) == 1)
                {
                    num10 = num12;
                }
                else
                {
                    num10 = num11;
                }
                num9 = i * num2;
                graphics.DrawString(code.Substring(i, 1), font, brush, (float)num9, (float)num10);
            }
            graphics.Dispose();
            return TwistImage(image, true, (double)Security.Instance.VCodeTortuosity, 4.0);
        }

        public static void CreateValidateCodeImage(string code)
        {
            MemoryStream stream = new MemoryStream();
            Bitmap bitmap = CreateImageCode(code);
            bitmap.Save(stream, ImageFormat.Jpeg);
            ValidateCode.context.Response.ClearContent();
            ValidateCode.context.Response.ContentType = "image/Jpeg";
            ValidateCode.context.Response.BinaryWrite(stream.GetBuffer());
            stream.Close();
            stream = null;
            bitmap.Dispose();
            bitmap = null;
        }

        public static string Generate()
        {
            ValidateCode.context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            string code = GenerateCode();
            CreateValidateCodeImage(code);
            return code;
        }

        public static string GenerateCode()
        {
            int vCodeLength = Security.Instance.VCodeLength;
            string[] strArray = Security.Instance.VCodeCharPool.Split(new char[] { ',' });
            string str = "";
            int index = -1;
            Random random = new Random((int)DateTime.Now.Ticks);
            for (int i = 0; i < vCodeLength; i++)
            {
                index = random.Next(0, strArray.Length - 1);
                str = str + strArray[index];
            }
            return str;
        }

        private static Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        {
            Bitmap image = new Bitmap(srcBmp.Width, srcBmp.Height);
            Graphics graphics = Graphics.FromImage(image);
            graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, image.Width, image.Height);
            graphics.Dispose();
            double num = bXDir ? ((double)image.Height) : ((double)image.Width);
            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    double a = 0.0;
                    a = bXDir ? ((6.2831853071795862 * j) / num) : ((6.2831853071795862 * i) / num);
                    a += dPhase;
                    double num5 = Math.Sin(a);
                    int x = 0;
                    int y = 0;
                    x = bXDir ? (i + ((int)(num5 * dMultValue))) : i;
                    y = bXDir ? j : (j + ((int)(num5 * dMultValue)));
                    Color pixel = srcBmp.GetPixel(i, j);
                    if (((x >= 0) && (x < image.Width)) && ((y >= 0) && (y < image.Height)))
                    {
                        image.SetPixel(x, y, pixel);
                    }
                }
            }
            return image;
        }
    }

}